<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>组队管理</title>
    <script type="text/javascript">
      var newTeamWin;
      var editTeamWin;
      var teamId;
      Ext.onReady(function(){
        //组队管理---------------------------------------------------------
        //生成有关组队的Grid
        var teamDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/team/getTeams'
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'team'
          },[
            {name:'id'},
            {name:'name'},
            {name:'is_locked'},
            {
              name:'manager_name',
              mapping: 'managers',
              convert: function(managers){
                if(managers.length>0){
                  var name = " ";
                  for(var i=0;i < managers.length;i++){
                    name += " " + managers[i].name
                  }
                  return name
                }else{
                  return '未指定'
                }
              }
            }
          ]
        )
        });

        var teamSM = new Ext.grid.CheckboxSelectionModel();
        var teamCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          teamSM,
          {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderIsLucked,
            width: 70
          },
          {header:'组队名称',sortable:true,dataIndex:'name',width:170},
          {header:'控制人员',sortable:true,dataIndex:'manager_name',width:850}
        ]);

        var teamGrid =  new Ext.grid.GridPanel({
          ds: teamDS,
          cm: teamCM,
          sm: teamSM,
          width: screenWidth-240,
          height: screenHeight-250,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: teamDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加组队',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newTeamWin = new Ext.Window({
                  layout: 'fit',
                  width:267,
                  height:140,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加组队',
                  autoLoad: {
                    url: "/team/newTeam",
                    scripts: true
                  }
                });
                newTeamWin.on("destroy",function(){
                  teamDS.reload();
                });
                newTeamWin.resizable = false;
                newTeamWin.show();
              }
            },{
              text: '编辑组队',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = teamGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  teamId = records[0].get('id');
                  editTeamWin = new Ext.Window({
                    title: '编辑组队',
                    layout:'fit',
                    width:590,
                    height:395,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/team/editTeam?id=" + teamId,
                      scripts: true
                    }
                  });
                  editTeamWin.on("destroy",function(){
                    teamDS.reload();
                  });
                  editTeamWin.resizable = false;
                  editTeamWin.show();
                }
              }
            }
            ,
            {
              text:'锁定或锁定组队',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(teamGrid.getSelectionModel().hasSelection()){
                  var rs=teamGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var teamName='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    teamName+=data.name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);

                  Ext.Ajax.request({
                    url:'/team/doLock',
                    method:'post',
                    waitMsg: '正在锁定或锁定班组...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        teamDS.reload();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要锁定或锁定的组队');
                }
              }
            }
          ]
        });
        teamGrid.render('teamList');
        teamDS.load();
      })
    </script>
  </head>
  <body>
    <div id="teamList"></div>
  </body>
</html>
